<?php
namespace jy_renwushop;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	分销插件所有金额字段均保存整形，精确到分。显示时，除以100然后保留两位小数即可
	佣金比例字段保存整形，值为万分数，相当于百分数的精确到小数点后2位。即展示到前端时，需要除以100，换算成百分数显示
*/
class member extends \plugin{
	public function checklogin($storeid='',$area_agentid='',$area_code=''){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			model('core/core')->checklogin();
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_renwushop/myhome/');
			}else{
				$user=pdo_get('jy_renwushop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'uniacid'=>$_SESSION['uniacid'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_renwushop_member',$insert);
				}
			}
		}
	}
	public function getdetail($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$user=pdo_get('jy_renwushop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return $user;
	}
	//type 0充值，1消费，2冻结，3解冻，4冻结消费，5退款，6提现,7返现
	public function recharge($mid=0,$fee=0,$type=0,$remark=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!intval($fee)){
			return false;
		}
		$logtype=5;
		$member=pdo_get('jy_renwushop_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$update=array();
		if($type==0){
			$update['baofee +=']=$fee;
			$msg='充值:￥'.$fee;
		}elseif($type==1){
			$update['baofee -=']=$fee;
			$msg='消费:￥'.$fee;
		}elseif($type==2){
			if($member['baofee']<$fee){
				exi('保证金不足，不足冻结额！','error');
			}
			$update['baofee -=']=$fee;
			$update['freeze +=']=$fee;
			$msg='冻结:￥'.$fee;
		}elseif($type==3){
			$update['baofee +=']=$fee;
			$msg='解冻:￥'.$fee;
			if($member['freeze']<$fee){
				$update['freeze']=0;
			}else{
				$update['freeze -=']=$fee;
			}
		}elseif($type==4){
			$msg='任务结算解冻消费:￥'.$fee;
			if($member['freeze']<$fee){
				$update['freeze']=0;
			}else{
				$update['freeze -=']=$fee;
			}
		}elseif($type==5){
			$update['baofee +=']=$fee;
			$msg='退款:￥'.$fee;
		}elseif($type==6){
			$update['baofee -=']=$fee;
			$msg='提现到余额:￥'.$fee;
		}elseif($type==7){
			$update['baofee +=']=$fee;
			$msg='参与返现:￥'.$fee;
			$logtype=7;
		}
		$user=pdo_update('jy_renwushop_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$params=array(
			'msg'=>$msg,
			'status'=>1,
			'mid'=>$mid,
		);
		$this->model('model')->set_log($params,$logtype);
		return true;
	}
	public function getfee($orderid){
		$order=pdo_get('jy_renwushop_renwuorder',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		$renwu=pdo_get('jy_renwushop_renwu',array('id'=>$order['renwuid'],'uniacid'=>$_SESSION['uniacid']));
		$this->model('member')->recharge($renwu['mid'],$order['fee'],4,'任务结算');
		$basic=$this->plugin_setting('basic');
		$koufee=$basic['order_kou']*$order['fee']/100;
		$getfee=$order['fee']-$koufee;
		if($koufee){
			pdo_update('jy_renwushop_renwuorder',array('koufee'=>$koufee),array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		}
		model('core/member')->member_credit($order['mid'],'credit2',$getfee,true,'jy_renwushop','完成任务获得奖励');
		$params=array(
			'mid'=>$order['mid'],
			'msg'=>'完成任务获得奖励',
			'fee'=>$getfee,
			'status'=>1,
			'renwuid'=>$order['renwuid'],
			'isadd'=>1,
		);
		$this->model('model')->set_feelog($params,0);
		$this->model('renwu')->canfen($order);
		return true;
	}
}